(ns blade-art-online.core.skill-logic
 (require [blade-art-online.core.skills :refer [skills buffs]]))

; tested
; buff处理skill,src,tar
(defn buffs-process 
 [buff-hook-key the-buffs all]
 (reduce (fn [product buff] ((buff-hook-key buff) product)) all the-buffs))

; tested
(defn buffkeys-to-buffs 
 [buffkeys]
 (map #(% buffs) buffkeys))

(defn produce-effect
 ([{:keys [skill] :as all}]
   (produce-effect skill all))
 ; tested
 ([skill all] 
  (assoc all :effect ((:produce-effect skill) all))))

; tested
(defn real-damage
 [defence old-damage]
 (max 1 (- old-damage defence)))

(defn safe-damage?
 [damage]
 (= damage 0))

(defn damage-on-tar
 ([{:keys [effect tar] :as all}]
  (assoc all :tar (damage-on-tar (:damage effect) tar)))
 ; tested
 ([damage tar]
  (if (safe-damage? damage) 
      tar
      (assoc tar :hp (- (:hp tar) (real-damage (:def tar) damage))))))

(defn affect
 [all]
 (-> all
     damage-on-tar))

(defn filter-result
 [{:keys [src tar]}]
 {:src src :tar tar})

(defn direct-affect
 ([{:keys [skill] :as all}] (direct-affect skill all))
 ([skill all] ((:affect skill) all)))

(defn life-spawn
 [& {:keys [hp mp str def buffkeys]
     :or {hp 10 mp 10 str 1 def 1 buffkeys []}}]
 {:hp hp
  :mp mp
  :str str
  :def def
  :buffkeys buffkeys}) 

(defn wrap-skill
 [{:keys [skill-key] :as all}]
 (assoc all :skill (skill-key skills)))

(defn use-skill
 [& {:keys [skill-key src tar] :as all}]
 (-> all
     wrap-skill
     direct-affect
     produce-effect
     affect
     filter-result))